#!/bin/python

# --------------------------------------
# VIDEO BUDDHA 2 - edit selected definition window
# --------------------------------------
# Ranames files based on a specific regex pattern in 
#  show_list.db and based on a specific list of file 
#  extensions.
# --------------------------------------

import wx

import renamer.db.database

class WinEditSelectedDef(wx.Frame):
	
	def __init__(self, id):
		wx.Frame.__init__(self, wx.GetApp().TopWindow, wx.ID_ANY, u"Edit Definition", wx.DefaultPosition, wx.Size(300, 130), wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX))
		
		# Database class
		self.db = renamer.db.database.Database()
	
		# Lookup id
		# Make sure it's a valid id
		#if id == -1: id = 1
		self.id = id
		
		# Application icon
		favicon = wx.Icon('res/favicon.ico', wx.BITMAP_TYPE_ICO, 16, 16)
		self.SetIcon(favicon)
		
		self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
		
		sizer_top = wx.BoxSizer(wx.VERTICAL)
		
		self.panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
		sizer_main = wx.BoxSizer(wx.VERTICAL)
		
		sizer_oldname = wx.BoxSizer( wx.HORIZONTAL )
		
		self.m_staticText1 = wx.StaticText(self.panel, wx.ID_ANY, u"Old Name", wx.DefaultPosition, wx.Size(65, -1), wx.ALIGN_RIGHT)
		self.m_staticText1.Wrap(-1)
		sizer_oldname.Add(self.m_staticText1, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)
		
		# Old name
		self.text_ctrl_oldname = wx.TextCtrl(self.panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_oldname.Add(self.text_ctrl_oldname, 1, wx.ALL, 5)
		
		sizer_main.Add(sizer_oldname, 0, wx.EXPAND, 5)
		
		sizer_newname = wx.BoxSizer(wx.HORIZONTAL)
		
		self.m_staticText2 = wx.StaticText(self.panel, wx.ID_ANY, u"New Name", wx.DefaultPosition, wx.Size(65, -1), wx.ALIGN_RIGHT)
		self.m_staticText2.Wrap(-1)
		sizer_newname.Add(self.m_staticText2, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)
		
		# New name
		self.text_ctrl_newname = wx.TextCtrl(self.panel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_newname.Add(self.text_ctrl_newname, 1, wx.ALL, 5)
		
		# Fill it with db info
		rows = self.db.retrieveId(id)
		
		for row in rows:
			self.text_ctrl_oldname.SetValue(row[1])
			self.text_ctrl_newname.SetValue(row[2])
		
		sizer_main.Add(sizer_newname, 0, wx.EXPAND, 5)
		
		sizer_buttons = wx.BoxSizer(wx.HORIZONTAL)
		
		sizer_buttons.AddSpacer((0, 0), 1, wx.EXPAND, 5)
		
		self.button_edit = wx.Button(self.panel, wx.ID_ANY, u"Edit", wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_buttons.Add(self.button_edit, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)
		
		self.cancelButton = wx.Button(self.panel, wx.ID_ANY, u"Cancel", wx.DefaultPosition, wx.DefaultSize, 0)
		sizer_buttons.Add(self.cancelButton, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
		
		sizer_main.Add(sizer_buttons, 1, wx.EXPAND | wx.ALIGN_RIGHT, 5)
		
		self.panel.SetSizer(sizer_main)
		self.panel.Layout()
		sizer_main.Fit(self.panel)
		sizer_top.Add(self.panel, 1, wx.ALL | wx.EXPAND, 0)
		
		self.SetSizer(sizer_top)
		self.Layout()
		
		self.Centre(wx.BOTH)
		
		self.button_edit.Bind(wx.EVT_BUTTON, self.editDefinition)
		self.cancelButton.Bind(wx.EVT_BUTTON, self.onCancel)
	
	def __del__(self):
		self.Close()
		
	def onCancel(self, event):
		self.Destroy()
		
	def editDefinition(self, event):
		oldname = self.text_ctrl_oldname.GetValue()
		newname = self.text_ctrl_newname.GetValue()
		
		if oldname == '' or newname == '':
			dialog = wx.MessageDialog(self, "One or both of the text fields is / are empty.", "Error!", wx.OK)
			dialog.ShowModal()
			dialog.Destroy()
			
			return False
		else:
			# Update the listings
			self.db.update(self.id, oldname, newname)
			
			self.Close()